package cn.hysh.appium.keyworddriver;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.DecimalFormat;


//本类主要实现后缀为 xlsx 的 excel  文件操作  
public class ExcelUtil {

	private static XSSFSheet ExcelWSheet;
	private static XSSFWorkbook ExcelWBook;
	private static XSSFCell Cell;
	private static XSSFRow Row;

	public static void setExcelFile(String Path){
			 
		FileInputStream ExcelFile;
		try {
			ExcelFile = new FileInputStream(Path);
			System.out.println(Path);
			ExcelWBook = new XSSFWorkbook(ExcelFile);

		} catch (Exception e) {
			TestSuiteByExcel.testResult = false;
		    System.out.println("Excel 路径设定失败xxxxxx");
			e.printStackTrace();
		}

	}
	public static void setExcelFile(String Path, String SheetName)
			 {
		FileInputStream ExcelFile;
		try {
			ExcelFile = new FileInputStream(Path);
			ExcelWBook = new XSSFWorkbook(ExcelFile);
			ExcelWSheet = ExcelWBook.getSheet(SheetName);

		} catch (Exception e) {
			TestSuiteByExcel.testResult = false;
		    System.out.println("Excel 路径设定失败！");
			e.printStackTrace();
		}

	}

	public static String getCellData(String SheetName,int RowNum, int ColNum) {
		ExcelWSheet = ExcelWBook.getSheet(SheetName);
		try {
			Cell = ExcelWSheet.getRow(RowNum).getCell(ColNum);
			//String CellData = Cell.getCellType() == XSSFCell.CELL_TYPE_STRING ? Cell.getStringCellValue() : String.valueOf(Math.round(Cell.getNumericCellValue()));
			String CellData="";
			if(Cell!=null){
				if(Cell.getCellType() == XSSFCell.CELL_TYPE_STRING){
					CellData=Cell.getStringCellValue();
				}else if(Cell.getCellType() ==XSSFCell.CELL_TYPE_NUMERIC){
					DecimalFormat df = new DecimalFormat("0");
					CellData = df.format(Cell.getNumericCellValue());
				}
			}else{
				System.out.println("第"+RowNum+"行"+"第"+ColNum+"Lie");
			}
			return CellData;

		} catch (Exception e) {
			TestSuiteByExcel.testResult = false;
			e.printStackTrace();
			return "";
		}
	}

	public static int getLastRowNum() {
		return ExcelWSheet.getLastRowNum();
	}
	public static int getRowCount(String SheetName){
		ExcelWSheet = ExcelWBook.getSheet(SheetName);
		int number=ExcelWSheet.getLastRowNum();
		return number;
	}

	public static int getFirstRowContainsTestCaseID(String sheetName,String testCaseName, int colNum) {
		int i;	
		try{
		
		ExcelWSheet = ExcelWBook.getSheet(sheetName);
			int rowCount = ExcelUtil.getRowCount(sheetName);
			for (i=0 ; i<rowCount; i++){
				//System.out.println(getCellData(sheetName,i,colNum));
				if  (ExcelUtil.getCellData(sheetName,i,colNum).equalsIgnoreCase(testCaseName)){
					break;
				}
			}
			return i;
		}catch (Exception e){
			TestSuiteByExcel.testResult = false;
			return 0;
		}
			
	}
	public static int getTestCaseLastStepRow(String SheetName, String testCaseID, int testCaseStartRowNumber){
		try{
		ExcelWSheet = ExcelWBook.getSheet(SheetName);
		/* 从包含指定测试用例序号的第一行开始逐行遍历，直到某一行不出现指定测试用例序号，
		      此时的遍历次数就是次测试用例步骤的个数  */
		for(int i=testCaseStartRowNumber;i<ExcelUtil.getRowCount(SheetName)-1;i++){
			//System.out.println(ExcelUtil.getCellData(SheetName,i, Constants.Col_TestCaseID));
			System.out.println(i+"");
			try {
				if(!testCaseID.equals(ExcelUtil.getCellData(SheetName,i, Constants.Col_TestCaseID))){
					int number = i;
					return number;
				}
			} catch (Exception e) {
			}

		}
		int number=ExcelWSheet.getLastRowNum()-1;
		return number;
		}catch(Exception e){
			TestSuiteByExcel.testResult = false;
			return 0;
		}
	}
	// 在 excel 文件的执行单元格中写入数据，此函数只支持后缀为  xlsx 的 excel 文件写入
		public static void setCellData(String SheetName,int RowNum, int ColNum, String Result) {
			ExcelWSheet = ExcelWBook.getSheet(SheetName);
			try {
				Row = ExcelWSheet.getRow(RowNum);
				Cell = Row.getCell(ColNum, Row.RETURN_BLANK_AS_NULL);

				if (Cell == null) {
					Cell = Row.createCell(ColNum);
					Cell.setCellValue(Result);

				} else {
					Cell.setCellValue(Result);

				}
				FileOutputStream fileOut = new FileOutputStream(
						Constants.Path_ExcelFile);
				ExcelWBook.write(fileOut);
				fileOut.flush();
				fileOut.close();

			} catch (Exception e) {
				TestSuiteByExcel.testResult = false;
				e.printStackTrace();
			}
		}
		public static void main(String[] args) {
			setExcelFile("configs\\test.xlsx");
			System.out.println(getTestCaseLastStepRow("test","退出01",8));
		}
}
